package com.dc.ftp.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.dc.ftp.application.MyApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * Created by Administrator on 2018/1/9.
 */

public class FileUtils {

    /**
     * 复制asset文件到指定目录
     * @param oldPath  asset下的路径
     * @param newPath  SD卡下保存路径
     */
    private static  Context context= MyApplication.getInstance();

    public static void CopyAssets(String oldPath, String newPath) {
        try {
            // 如果是文件
            File file = new File(newPath);
            if (!file.exists()){
                file.mkdirs();// 如果文件夹不存在，则递归
            }


            if (file.length()!=0){
                MyLog.e("文件已存在外部存储---文件绝对路径---->"+file.getAbsolutePath());
                return;
            }

                InputStream is = context.getAssets().open(oldPath);
                FileOutputStream fos = new FileOutputStream(new File(newPath));
                byte[] buffer = new byte[1024];
                int byteCount = 0;
                while ((byteCount = is.read(buffer)) != -1) {// 循环从输入流读取
                    // buffer字节
                    fos.write(buffer, 0, byteCount);// 将读取的输入流写入到输出流
                }
                fos.flush();// 刷新缓冲区
                is.close();
                fos.close();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }





    //首先，复制单个文件：
    public static Boolean CopyAssetsFile(String filename, String des) {
        Boolean isSuccess = true;
        //复制安卓apk的assets目录下任意路径的单个文件到des文件夹，注意是否对des有写权限
        AssetManager assetManager = context.getAssets();

        InputStream in = null;
        OutputStream out = null;
        try {
            in = assetManager.open(filename);
            String newFileName = des + "/" + filename;
            out = new FileOutputStream(newFileName);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e) {
            e.printStackTrace();
            isSuccess = false;
        }

        return isSuccess;

    }

    //然后是文件夹的复制，需要调用上面的函数：
    public static Boolean CopyAssetsDir(String src,String des) {
        //复制安卓Assets下的“非空目录”到des文件夹，注意是否对des有写权限
        Boolean isSuccess = true;
        String[] files;
        try
        {
            files = context.getResources().getAssets().list(src);
        }
        catch (IOException e1)
        {
            return false;
        }

        if(files.length==0){
            isSuccess = CopyAssetsFile(src,des);//对于文件直接复制
            if(!isSuccess)
                return isSuccess;
        }
        else{
            File srcfile = new File(des+"/"+src);
            if(!srcfile.exists()){
                if(srcfile.mkdirs()){//对于目录自行创建
                    for(int i=0;i<files.length;i++){//新浪博客的限制，这里的小于号请自行替换为英文的
                        isSuccess = CopyAssetsDir(src + "/"+files[i], des);//递归调用
                        if(!isSuccess)
                            return isSuccess;
                    }
                }
                else{
                    return false;
                }
            }

        }
        return isSuccess;
    }






}
